<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Home extends MY_Controller {
    /**
     * @var MerchantUserModel
     */
    public $mMerchant = null;
    /**
     * @var ConsumerOrderModel
     */
    public $mOrder = null;
    /**
     * @var ConsumerProductModel
     */
    public $mProduct = null;
    /**
     * @var MerchantAdsModel
     */
    public $mAds = null;


    public function __construct(){
        parent::__construct();
        $this->loadModel('ConsumerOrderModel', 'mOrder', 'consumer');
        $this->loadModel('MerchantUserModel', 'mMerchant', 'merchant');
        $this->loadModel('MerchantAdsModel', 'mAds', 'merchant');

    }

    /**
     * 首页
     */
    public function index(){

        $this->loadModel('ConsumerProductModel', 'mProduct', 'consumer');
        $this->loadModel('AdmFeedbackModel', 'mFeedback', 'admin');
        $this->loadModel('DlnFeedbackModel', 'mDlnFeed', 'deliveryman');

        $time = date('Y-m', time());
        $time = strtotime($time.'-1 00:00:00'); // 本月一号零点时间

        $_GET['ads_status'] = 1;
        $_GET['status'] = 1;


        $data = array(
            'totalMerchant' => $this->mMerchant->getTotalMerchantShop(0, $this->countyItem), // 所有商家
            'monthTotalMerchant' => $this->mMerchant->getTotalMerchantShop($time, $this->countyItem), // 本月入驻商家
            'totalOrder' => $this->mOrder->getTotalOrder(0, 0, $this->countyItem), // 所有销售额
            'monthTotalOrder' => $this->mOrder->getTotalOrder($time, 0, $this->countyItem), // 本月销售额
            'totalProduct' => $this->mProduct->getTotalProduct(0, $this->countyItem), // 所有商品
            'monthTotalProduct' => $this->mProduct->getTotalProduct($time, $this->countyItem), // 本月新增商品
            'otdOrder' => $this->mOrder->getOverThreeDaysOrderList(true, $this->countyItem), // 超3天未发货订单
            'service' => $this->mOrder->getTwentyFourHoursUntreatedOrderServiceList(true, $this->countyItem), // 24小时未处理售后订单
            'merchant' => $this->mMerchant->getMerchantAuthenticationList(true, $this->countyItem), // 商户认证申请
            'status' => $this->mOrder->status(),
            'expenseType' => $this->mOrder->expenseType(),
            'payType' => $this->mOrder->payType(),
            'position' => getCustomConfig('merchant_position'),
            'shopStatus'=>$this->mMerchant->getShopStatus(),
            'feedbackCount'=>$this->mFeedback->getFeedbackCount($this->countyItem), //未处理的意见反馈数量
            'dln_feedback'=>$this->mDlnFeed->getFeedbackCount($this->countyItem), //未处理的意见反馈数量
            'shop_type'=>$this->mMerchant->config['shop_type'],
        );
        $this->view('home', $data, 'datatable,validation,datepicker');
    }

    /**
     * 获取超过3天未发货订单
     */
    public function getOverThreeDaysOrderList(){
        $data = $this->mOrder->getOverThreeDaysOrderList(false, $this->countyItem);
        if ($data['data']){
            foreach ($data['data'] as $k => $v){
                $data['aaData'][$k][] = $v['code'];
                $data['aaData'][$k][] = $v['merchant'];
                $data['aaData'][$k][] = $v['consumer'];
                $data['aaData'][$k][] = $this->mOrder->expenseType($v['expense_type']);
                $data['aaData'][$k][] = $this->mOrder->payType($v['pay_type']);
                $data['aaData'][$k][] = $v['total_amount'];
                $data['aaData'][$k][] = dateFormat($v['addtime']);
                if ($v['expense_type'] == 2){
                    $data['aaData'][$k][] = $this->mOrder->storeStatus($v['status'], true);
                }else{
                    $data['aaData'][$k][] = $this->mOrder->status($v['status'], true);
                }
                $btnConfig = adminCreateBtn(array(
                    'label' => '查看',
                    'attr' => array(
                        'href' => getUrl('order/home/details?id='.$v['id']),
                        'class' => 'btn green-stripe btn-xs default'
                    ),
                    'tag' => 'a',
                    'permissions' => array(
                        'module' => 'classes',
                        'key' => 'edit'
                    )
                ));
                $data['aaData'][$k][] = $btnConfig;
            }
        }
        unset($data['data']);
        echo json_encode($data);
    }

    /**
     * 24小时未处理售后订单
     */
    public function getTwentyFourHoursUntreatedOrderServiceList(){
        $data = $this->mOrder->getTwentyFourHoursUntreatedOrderServiceList(false, $this->countyItem);
        if ($data['data']){
            foreach ($data['data'] as $k => $v){
                $data['aaData'][$k][] = $v['code'];
                $data['aaData'][$k][] = $v['order_code'];
                $data['aaData'][$k][] = $v['shop_name'];
                $data['aaData'][$k][] = $v['name'];
                $data['aaData'][$k][] = $v['remark'];
                $data['aaData'][$k][] = $v['refund_amount'];
                $data['aaData'][$k][] = dateFormat($v['addtime']);
                $data['aaData'][$k][] = $this->mOrder->serviceType($v['status'], true);
                $btnConfig = adminCreateBtn(array(
                    'label' => '查看',
                    'attr' => array(
                        'href' => getUrl('order/service/details?code='.$v['code']),
                        'class' => 'btn green-stripe btn-xs default'
                    ),
                    'tag' => 'a',
                    'permissions' => array(
                        'module' => 'classes',
                        'key' => 'edit'
                    )
                ));
                $data['aaData'][$k][] = $btnConfig;
            }
        }
        unset($data['data']);
        echo json_encode($data);
    }

    /**
     * 商户认证申请
     */
    public function getMerchantAuthenticationList(){
        $data = $this->mMerchant->getMerchantAuthenticationList(false, $this->countyItem);
        $shopType = $this->mMerchant->config['shop_type'];
        if ($data['data']){
            foreach ($data['data'] as $k => $v){
                $data['aaData'][$k][] = $v['shop_name'];
                $data['aaData'][$k][] = $v['mobile'];
                $data['aaData'][$k][] = $shopType[$v['shop_type']];
                $data['aaData'][$k][] = $v['location'];
                $data['aaData'][$k][] = $v['id_name'];
                $data['aaData'][$k][] = $v['identification'];
                $data['aaData'][$k][] = dateFormat($v['shop_addtime']);
                $data['aaData'][$k][] = $this->mMerchant->getShopStatus($v['shop_status'], true);
                $btnConfig = adminCreateBtn(array(
                    'label' => '查看',
                    'attr' => array(
                        'href' => getUrl('member/merchant/details?code='.$v['id']),
                        'class' => 'btn green-stripe btn-xs default'
                    ),
                    'tag' => 'a',
                    'permissions' => array(
                        'module' => 'classes',
                        'key' => 'edit'
                    )
                ));
                $btnConfig .= ' '.adminCreateBtn(array(
                        'label' => '认证通过',
                        'attr' => array(
                            'href' => getUrl('member/merchant/shopStatus?id='.$v['id'].'&status=2'),
                            'class' => 'ajax-request-btn btn btn-xs blue-stripe default',
                            'alert-message'=>'确定要通过“'.$v['shop_name'].'”的认证?',
                        ),
                        'tag' => 'a',
                        'permissions' => array(
                            'module' => 'classes',
                            'key' => 'edit'
                        )
                    ));
                $btnConfig .=' '.adminCreateBtn(array(
                        'label' => '拒绝认证',
                        'attr' => array(
                            'href' => getUrl('member/merchant/shopStatus?id='.$v['id'].'&status=1'),
                            'class' => 'ajax-request-btn btn btn-xs red-stripe default',
                            'alert-message'=>'确定要拒绝“'.$v['shop_name'].'”的认证?',
                        ),
                        'tag' => 'a',
                        'permissions' => array(
                            'module' => 'classes',
                            'key' => 'edit'
                        )
                    ));
                $data['aaData'][$k][] = $btnConfig;
            }
        }
        unset($data['data']);
        echo json_encode($data);
    }

    /**
     * 意见反馈
     */
    public function getMerchantFeedbackList(){
        $this->loadModel('AdmFeedbackModel', 'mFeedback', 'admin');
        $data = $this->mFeedback->adminPcGetList($this->countyItem);
        foreach ($data['data'] as $k => $v){
            $data['aaData'][$k][] = $v['addtime'];
            $data['aaData'][$k][] = $v['merchant_name'];
            $data['aaData'][$k][] = '<textarea readonly class="form-control remaks-item sss textarea_height">'.$v['content'].'</textarea>';
            $data['aaData'][$k][] = $this->mFeedback->getStatus($v['status'], true);
            $btnConfig = '';
            if($v['status'] == 1) {
                $btnConfig = adminCreateBtn(array(
                    'label' => ' 回复',
                    'icon' => '',
                    'attr' => array(
                        'href' => 'javascript:;',
                        'class' => 'edit-btn btn default btn-xs blue-stripe',
                        'data-id' => $v['id'],
                        'data-type' => 1,
                    ),
                    'tag' => 'a',

                ));
            }

            $data['aaData'][$k][] = $btnConfig;
        }
        unset($data['data']);
        echo json_encode($data);
    }

    public function getDlnFeedbackList(){
        $this->loadModel('DlnFeedbackModel', 'mFeedback', 'deliveryman');
        $this->loadModel('DlnUserModel', 'mMUser', 'deliveryman');

        $dateFrom = trim(get('send_date_from'));
        $dateTo = trim(get('send_date_to'));
        if($dateFrom){
            $this->db->where('f.addtime >=', strtotime($dateFrom));
        }

        if($dateTo){
            $this->db->where('f.addtime <', strtotime('+1day', strtotime($dateTo)));
        }

        $this->db->select('f.id,u.name,f.content,f.reply,f.status,f.addtime')
            ->join($this->mMUser->tbUser.' u', 'u.id = f.deliveryman_id')
            ->where_in('u.county_id', $this->countyItem);
        $this->db->where('f.status', 1);
        $this->db->order_by('f.id desc');

        $data = $this->mFeedback->pagination($this->mFeedback->tbFeedback.' f');
        foreach ($data['data'] as $k => $v){
            $data['aaData'][$k][] = dateFormat($v['addtime'], true);
            $data['aaData'][$k][] = $v['name'];
            $data['aaData'][$k][] = '<textarea readonly class="form-control remaks-item sss textarea_height">'.$v['content'].'</textarea>';
            $data['aaData'][$k][] = $this->mFeedback->status($v['status'], true);
            $btnConfig = '';
            if($v['status'] == 1) {
                $btnConfig = adminCreateBtn(array(
                    'label' => ' 回复',
                    'icon' => '',
                    'attr' => array(
                        'href' => 'javascript:;',
                        'class' => 'edit-btn btn default btn-xs blue-stripe',
                        'data-id' => $v['id'],
                        'data-type' => 3,
                    ),
                    'tag' => 'a',

                ));
            }

            $data['aaData'][$k][] = $btnConfig;
        }
        unset($data['data']);
        echo json_encode($data);
    }

    /**
     * 回复
     */
    public function reply(){
        $this->loadModel('AdmFeedbackModel', 'mFeedback', 'admin');

        //表单验证
        $vConf = array(
            array('id', '反馈id', 'required|is_natural'),
            array('reply', '回复内容', 'required'),
            array('data_type', '类型', 'required|in_list[1,2,3]'),
        );
        $this->formDataValidation($vConf);

        $id = (int)post('id');
        $reply = post('reply');
        $type = post('data_type');
        $data['reply'] = $reply;
        $data['status'] = 2;

        if($type == 3){
            $this->loadModel('DlnFeedbackModel', 'mFeed', 'deliveryman');
            $this->db->where('id', $id);
            if($this->mFeed->update($this->mFeed->tbFeedback, $data)){
                $this->ajaxReturn(1, '回复成功',array('type' => $type,'id' => $id));
            }
        }else{
            if($this->mFeedback->save($data,$id)){
                $this->ajaxReturn(1, '回复成功',array('type' => $type,'id' => $id));
            }
        }

        $this->ajaxReturn(0, '回复失败');
    }
}
